<?php
namespace app\banquet\controller;
use app\AdminRest;


use app\banquet\model\Config;
use app\banquet\model\Order;
use app\banquet\model\Package;
use app\banquet\model\Place;
use app\banquet\model\PlaceDate;
use app\banquet\model\Refund;
use app\card\model\Company;
use app\card\model\User;
use think\App;
use think\facade\Db;


class AdminOrder extends AdminRest
{

    protected $model;

    protected $refund_model;

    protected $package_model;



    public function __construct(App $app) {

        parent::__construct($app);

        $this->model  = new Order();

        $this->refund_model  = new Refund();

        $this->package_model = new Package();

        $this->model->autoCancelOrder($this->_uniacid);


    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-07 17:14
     * @功能说明:订单列表
     */
    public function orderList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['place_title'])){

            $dis[] = ['a.place_title','like','%'.$input['place_title'].'%'];
        }

        if(!empty($input['order_code'])){

            $dis[] = ['a.order_code','like','%'.$input['order_code'].'%'];
        }

        if(!empty($input['phone'])){

            $dis[] = ['b.phone','like','%'.$input['phone'].'%'];
        }

        if(!empty($input['start_time'])){

            $dis[] = ['a.time_date','between',"{$input['start_time']},{$input['end_time']}"];
        }

        if(!empty($input['company_id'])){

            $dis[] = ['a.company_id','=',$input['company_id']];
        }

        if(!empty($input['have_eva'])){

            $dis[] = ['a.have_eva','=',$input['have_eva']];

            $input['pay_type'] = 0;

        }

        if(!empty($input['pay_type'])){

            $dis[] = ['a.pay_type','=',$input['pay_type']];

            $dis[] = ['a.have_eva','=',0];

        }

        $data = $this->model->adminList($dis,$input['limit']);

        if(!empty($data['data'])){

            $user_model = new User();

            foreach ($data['data'] as &$v){

                $v['user_info'] = $user_model->where(['id'=>$v['user_id']])->field('nickName')->find();

                $v['staff_name']= lbUserName($v['staff_id']);

                $v['week']      = changeWeek(date('w',$v['time_date']));

                $v['time_type'] = $v['time_type']==1?'中午':'晚上';
            }

        }

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-07 17:43
     * @功能说明:订单详情
     */
    public function orderInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->model->dataInfo($dis);

        $user_model = new User();

        $data['user_info'] = $user_model->where(['id'=>$data['user_id']])->field('nickName')->find();

        $data['staff_name']= lbUserName($data['staff_id']);

        $data['week']      = changeWeek(date('w',$data['time_date']));

        $data['time_type'] = $data['time_type']==1?'中午':'晚上';

        $data['phone']     = longbingGetUserAuthorizationPhone($data['user_id'],$this->_uniacid);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2022-01-07 17:50
     * @功能说明:退款订单列表
     */
    public function refundList(){

        $input = $this->_param;

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        if(!empty($input['place_title'])){

            $dis[] = ['a.place_title','like','%'.$input['place_title'].'%'];
        }

        if(!empty($input['order_status'])){

            $dis[] = ['a.status','=',$input['order_status']];

        }else{

            $dis[] = ['a.status','>',-1];

        }

        if(!empty($input['pay_order_code'])){

            $dis[] = ['b.order_code','like','%'.$input['pay_order_code'].'%'];

        }

        $data = $this->refund_model->orderRefundList($dis,$input['limit']);

        if(!empty($data['data'])){

            $user_model = new User();

            foreach ($data['data'] as &$v){

                $v['user_info'] = $user_model->where(['id'=>$v['user_id']])->field('nickName')->find();

                $v['staff_name']= lbUserName($v['staff_id']);

            }

        }

        return $this->success($data);

    }


    /**\
     * @author chenniang
     * @DataTime: 2022-01-07 18:01
     * @功能说明：退款订单详情
     */
    public function refundInfo(){

        $input = $this->_param;

        $dis = [

            'id' => $input['id']
        ];

        $data = $this->refund_model->dataInfo($dis);

        $data['staff_name']= lbUserName($data['staff_id']);

        $user_model = new User();

        $data['user_info'] = $user_model->where(['id'=>$data['user_id']])->field('nickName')->find();

        $data['week']      = changeWeek(date('w',$data['time_date']));

        $data['time_type'] = $data['time_type']==1?'中午':'晚上';

        $data['phone']     = longbingGetUserAuthorizationPhone($data['user_id'],$this->_uniacid);

        $order_model = new Order();

        $pay_order   = $order_model->dataInfo(['id'=>$data['order_id']]);

        $data['pay_order_code'] = $pay_order['order_code'];

        $data['content'] = $pay_order['content'];

        return $this->success($data);

    }



    /**
     * @author chenniang
     * @DataTime: 2022-01-07 18:04
     * @功能说明:同意退款
     *
     */
    public function passRefundOrder(){

        $input = $this->_input;

        $res = $this->refund_model->passOrder($input['id'],$input['price'],$this->payConfig(),0,$input['text']);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }




    /**
     * @author chenniang
     * @DataTime: 2021-03-18 09:21
     * @功能说明:拒绝退款
     */
    public function noPassRefund(){

        $input = $this->_input;

        $res = $this->refund_model->noPassRefund($input['id']);

        if(!empty($res['code'])){

            $this->errorMsg($res['msg']);
        }

        return $this->success($res);

    }







}
